<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>List::Util - A selection of general-utility list subroutines - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/List::Util">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./List::Util.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/List::Util">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/List::Util">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/List::Util">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/List::Util">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/List::Util">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/List::Util">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/List::Util">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/List::Util">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/List::Util">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/List::Util">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/List::Util">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/List::Util">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/List::Util">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/List::Util">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/List::Util">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/List::Util">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/List::Util">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/List::Util">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/List::Util">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/List::Util">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/List::Util">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/List::Util">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/List::Util">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/List::Util">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/List::Util">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/List::Util">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/List::Util">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/List::Util">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/List::Util">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/List::Util">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/List::Util">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/List::Util">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/List::Util">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/List::Util">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/List::Util">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/List::Util">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/List::Util">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/List::Util">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/List::Util">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/List::Util">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/List::Util">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/List::Util">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/List::Util">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/List::Util">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/List::Util">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/List::Util">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/List::Util">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/List::Util">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/List::Util">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/List::Util">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/List::Util">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/List::Util">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/List::Util">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/List::Util">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/List::Util">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/List::Util">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/List::Util">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/List::Util">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/List::Util">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/List::Util">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/List::Util">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/List::Util">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/List::Util">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/List::Util">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/List::Util">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/List::Util">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/List::Util">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/List::Util">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/List::Util">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/List::Util">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/List::Util">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/List::Util">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/List::Util">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/List::Util">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/List::Util">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/List::Util">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/List::Util">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/List::Util">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/List::Util">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/List::Util">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./List::Util.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/List::Util">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/List::Util">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/List::Util">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/List::Util">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/List::Util">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/List::Util">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/List::Util">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/List::Util">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/List::Util">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/List::Util">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/List::Util">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/List::Util">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/List::Util">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/List::Util">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/List::Util">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/List::Util">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/List::Util">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/List::Util">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/List::Util">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/List::Util">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/List::Util">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/List::Util">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/List::Util">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/List::Util">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/List::Util">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/List::Util">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/List::Util">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/List::Util">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/List::Util">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/List::Util">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/List::Util">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/List::Util">5.35.1</a>
          <a class="dropdown-item active" href="./List::Util.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="./List::Util.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="./List::Util.html">List::Util</a>
          <div id="more">
            (<a href="./List::Util.txt">source</a>,
            <a href="https://metacpan.org/pod/List::Util">CPAN</a>)
          </div>
            <div id="moduleversion">version 1.55</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#LIST-REDUCTION-FUNCTIONS">LIST-REDUCTION FUNCTIONS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#reduce">reduce</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#reductions">reductions</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#any">any</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#all">all</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#none">none</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#notall">notall</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#first">first</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#max">max</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#maxstr">maxstr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#min">min</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#minstr">minstr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#product">product</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#sum">sum</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#sum0">sum0</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#KEY/VALUE-PAIR-LIST-FUNCTIONS">KEY/VALUE PAIR LIST FUNCTIONS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairs">pairs</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#unpairs">unpairs</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairkeys">pairkeys</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairvalues">pairvalues</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairgrep">pairgrep</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairfirst">pairfirst</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#pairmap">pairmap</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#OTHER-FUNCTIONS">OTHER FUNCTIONS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#shuffle">shuffle</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#sample">sample</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#uniq">uniq</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#uniqint">uniqint</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#uniqnum">uniqnum</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#uniqstr">uniqstr</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#head">head</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#tail">tail</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#CONFIGURATION-VARIABLES">CONFIGURATION VARIABLES</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#$RAND">$RAND</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#KNOWN-BUGS">KNOWN BUGS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#RT-%2395409">RT #95409</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#uniqnum()-on-oversized-bignums">uniqnum() on oversized bignums</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#SUGGESTED-ADDITIONS">SUGGESTED ADDITIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#SEE-ALSO">SEE ALSO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="./List::Util.html#COPYRIGHT">COPYRIGHT</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="./List::Util.html#NAME">#</a>NAME</h1>

<p>List::Util - A selection of general-utility list subroutines</p>

<h1 id="SYNOPSIS"><a class="permalink" href="./List::Util.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use List::Util qw(
  reduce any all none notall first reductions

  max maxstr min minstr product sum sum0

  pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap

  shuffle uniq uniqint uniqnum uniqstr
);</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="./List::Util.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p><code>List::Util</code> contains a selection of subroutines that people have expressed would be nice to have in the perl core, but the usage would not really be high enough to warrant the use of a keyword, and the size so small such that being individual extensions would be wasteful.</p>

<p>By default <code>List::Util</code> does not export any subroutines.</p>

<h1 id="LIST-REDUCTION-FUNCTIONS"><a class="permalink" href="./List::Util.html#LIST-REDUCTION-FUNCTIONS">#</a><a id="LIST"></a>LIST-REDUCTION FUNCTIONS</h1>

<p>The following set of functions all apply a given block of code to a list of values.</p>

<h2 id="reduce"><a class="permalink" href="./List::Util.html#reduce">#</a>reduce</h2>

<pre><code>$result = reduce { BLOCK } @list</code></pre>

<p>Reduces <code>@list</code> by calling <code>BLOCK</code> in a scalar context multiple times, setting <code>$a</code> and <code>$b</code> each time. The first call will be with <code>$a</code> and <code>$b</code> set to the first two elements of the list, subsequent calls will be done by setting <code>$a</code> to the result of the previous call and <code>$b</code> to the next element in the list.</p>

<p>Returns the result of the last call to the <code>BLOCK</code>. If <code>@list</code> is empty then <code>undef</code> is returned. If <code>@list</code> only contains one element then that element is returned and <code>BLOCK</code> is not executed.</p>

<p>The following examples all demonstrate how <code>reduce</code> could be used to implement the other list-reduction functions in this module. (They are not in fact implemented like this, but instead in a more efficient manner in individual C functions).</p>

<pre><code>$foo = reduce { defined($a)            ? $a :
                $code-&gt;(local $_ = $b) ? $b :
                                         undef } undef, @list # first

$foo = reduce { $a &gt; $b ? $a : $b } 1..10       # max
$foo = reduce { $a gt $b ? $a : $b } &#39;A&#39;..&#39;Z&#39;   # maxstr
$foo = reduce { $a &lt; $b ? $a : $b } 1..10       # min
$foo = reduce { $a lt $b ? $a : $b } &#39;aa&#39;..&#39;zz&#39; # minstr
$foo = reduce { $a + $b } 1 .. 10               # sum
$foo = reduce { $a . $b } @bar                  # concat

$foo = reduce { $a || $code-&gt;(local $_ = $b) } 0, @bar   # any
$foo = reduce { $a &amp;&amp; $code-&gt;(local $_ = $b) } 1, @bar   # all
$foo = reduce { $a &amp;&amp; !$code-&gt;(local $_ = $b) } 1, @bar  # none
$foo = reduce { $a || !$code-&gt;(local $_ = $b) } 0, @bar  # notall
   # Note that these implementations do not fully short-circuit</code></pre>

<p>If your algorithm requires that <code>reduce</code> produce an identity value, then make sure that you always pass that identity value as the first argument to prevent <code>undef</code> being returned</p>

<pre><code>$foo = reduce { $a + $b } 0, @values;             # sum with 0 identity value</code></pre>

<p>The above example code blocks also suggest how to use <code>reduce</code> to build a more efficient combined version of one of these basic functions and a <code>map</code> block. For example, to find the total length of all the strings in a list, we could use</p>

<pre><code>$total = sum map { length } @strings;</code></pre>

<p>However, this produces a list of temporary integer values as long as the original list of strings, only to reduce it down to a single value again. We can compute the same result more efficiently by using <code>reduce</code> with a code block that accumulates lengths by writing this instead as:</p>

<pre><code>$total = reduce { $a + length $b } 0, @strings</code></pre>

<p>The other scalar-returning list reduction functions are all specialisations of this generic idea.</p>

<h2 id="reductions"><a class="permalink" href="./List::Util.html#reductions">#</a>reductions</h2>

<pre><code>@results = reductions { BLOCK } @list</code></pre>

<p><i>Since version 1.54.</i></p>

<p>Similar to <code>reduce</code> except that it also returns the intermediate values along with the final result. As before, <code>$a</code> is set to the first element of the given list, and the <code>BLOCK</code> is then called once for remaining item in the list set into <code>$b</code>, with the result being captured for return as well as becoming the new value for <code>$a</code>.</p>

<p>The returned list will begin with the initial value for <code>$a</code>, followed by each return value from the block in order. The final value of the result will be identical to what the <code>reduce</code> function would have returned given the same block and list.</p>

<pre><code>reduce     { &quot;$a-$b&quot; }  &quot;a&quot;..&quot;d&quot;    # &quot;a-b-c-d&quot;
reductions { &quot;$a-$b&quot; }  &quot;a&quot;..&quot;d&quot;    # &quot;a&quot;, &quot;a-b&quot;, &quot;a-b-c&quot;, &quot;a-b-c-d&quot;</code></pre>

<h2 id="any"><a class="permalink" href="./List::Util.html#any">#</a>any</h2>

<pre><code>my $bool = any { BLOCK } @list;</code></pre>

<p><i>Since version 1.33.</i></p>

<p>Similar to <code>grep</code> in that it evaluates <code>BLOCK</code> setting <code>$_</code> to each element of <code>@list</code> in turn. <code>any</code> returns true if any element makes the <code>BLOCK</code> return a true value. If <code>BLOCK</code> never returns true or <code>@list</code> was empty then it returns false.</p>

<p>Many cases of using <code>grep</code> in a conditional can be written using <code>any</code> instead, as it can short-circuit after the first true result.</p>

<pre><code>if( any { length &gt; 10 } @strings ) {
    # at least one string has more than 10 characters
}</code></pre>

<p>Note: Due to XS issues the block passed may be able to access the outer @_ directly. This is not intentional and will break under debugger.</p>

<h2 id="all"><a class="permalink" href="./List::Util.html#all">#</a>all</h2>

<pre><code>my $bool = all { BLOCK } @list;</code></pre>

<p><i>Since version 1.33.</i></p>

<p>Similar to <a href="./List::Util.html#any">&quot;any&quot;</a>, except that it requires all elements of the <code>@list</code> to make the <code>BLOCK</code> return true. If any element returns false, then it returns false. If the <code>BLOCK</code> never returns false or the <code>@list</code> was empty then it returns true.</p>

<p>Note: Due to XS issues the block passed may be able to access the outer @_ directly. This is not intentional and will break under debugger.</p>

<h2 id="none"><a class="permalink" href="./List::Util.html#none">#</a>none</h2>

<h2 id="notall"><a class="permalink" href="./List::Util.html#notall">#</a>notall</h2>

<pre><code>my $bool = none { BLOCK } @list;

my $bool = notall { BLOCK } @list;</code></pre>

<p><i>Since version 1.33.</i></p>

<p>Similar to <a href="./List::Util.html#any">&quot;any&quot;</a> and <a href="./List::Util.html#all">&quot;all&quot;</a>, but with the return sense inverted. <code>none</code> returns true only if no value in the <code>@list</code> causes the <code>BLOCK</code> to return true, and <code>notall</code> returns true only if not all of the values do.</p>

<p>Note: Due to XS issues the block passed may be able to access the outer @_ directly. This is not intentional and will break under debugger.</p>

<h2 id="first"><a class="permalink" href="./List::Util.html#first">#</a>first</h2>

<pre><code>my $val = first { BLOCK } @list;</code></pre>

<p>Similar to <code>grep</code> in that it evaluates <code>BLOCK</code> setting <code>$_</code> to each element of <code>@list</code> in turn. <code>first</code> returns the first element where the result from <code>BLOCK</code> is a true value. If <code>BLOCK</code> never returns true or <code>@list</code> was empty then <code>undef</code> is returned.</p>

<pre><code>$foo = first { defined($_) } @list    # first defined value in @list
$foo = first { $_ &gt; $value } @list    # first value in @list which
                                      # is greater than $value</code></pre>

<h2 id="max"><a class="permalink" href="./List::Util.html#max">#</a>max</h2>

<pre><code>my $num = max @list;</code></pre>

<p>Returns the entry in the list with the highest numerical value. If the list is empty then <code>undef</code> is returned.</p>

<pre><code>$foo = max 1..10                # 10
$foo = max 3,9,12               # 12
$foo = max @bar, @baz           # whatever</code></pre>

<h2 id="maxstr"><a class="permalink" href="./List::Util.html#maxstr">#</a>maxstr</h2>

<pre><code>my $str = maxstr @list;</code></pre>

<p>Similar to <a href="./List::Util.html#max">&quot;max&quot;</a>, but treats all the entries in the list as strings and returns the highest string as defined by the <code>gt</code> operator. If the list is empty then <code>undef</code> is returned.</p>

<pre><code>$foo = maxstr &#39;A&#39;..&#39;Z&#39;          # &#39;Z&#39;
$foo = maxstr &quot;hello&quot;,&quot;world&quot;   # &quot;world&quot;
$foo = maxstr @bar, @baz        # whatever</code></pre>

<h2 id="min"><a class="permalink" href="./List::Util.html#min">#</a>min</h2>

<pre><code>my $num = min @list;</code></pre>

<p>Similar to <a href="./List::Util.html#max">&quot;max&quot;</a> but returns the entry in the list with the lowest numerical value. If the list is empty then <code>undef</code> is returned.</p>

<pre><code>$foo = min 1..10                # 1
$foo = min 3,9,12               # 3
$foo = min @bar, @baz           # whatever</code></pre>

<h2 id="minstr"><a class="permalink" href="./List::Util.html#minstr">#</a>minstr</h2>

<pre><code>my $str = minstr @list;</code></pre>

<p>Similar to <a href="./List::Util.html#min">&quot;min&quot;</a>, but treats all the entries in the list as strings and returns the lowest string as defined by the <code>lt</code> operator. If the list is empty then <code>undef</code> is returned.</p>

<pre><code>$foo = minstr &#39;A&#39;..&#39;Z&#39;          # &#39;A&#39;
$foo = minstr &quot;hello&quot;,&quot;world&quot;   # &quot;hello&quot;
$foo = minstr @bar, @baz        # whatever</code></pre>

<h2 id="product"><a class="permalink" href="./List::Util.html#product">#</a>product</h2>

<pre><code>my $num = product @list;</code></pre>

<p><i>Since version 1.35.</i></p>

<p>Returns the numerical product of all the elements in <code>@list</code>. If <code>@list</code> is empty then <code>1</code> is returned.</p>

<pre><code>$foo = product 1..10            # 3628800
$foo = product 3,9,12           # 324</code></pre>

<h2 id="sum"><a class="permalink" href="./List::Util.html#sum">#</a>sum</h2>

<pre><code>my $num_or_undef = sum @list;</code></pre>

<p>Returns the numerical sum of all the elements in <code>@list</code>. For backwards compatibility, if <code>@list</code> is empty then <code>undef</code> is returned.</p>

<pre><code>$foo = sum 1..10                # 55
$foo = sum 3,9,12               # 24
$foo = sum @bar, @baz           # whatever</code></pre>

<h2 id="sum0"><a class="permalink" href="./List::Util.html#sum0">#</a>sum0</h2>

<pre><code>my $num = sum0 @list;</code></pre>

<p><i>Since version 1.26.</i></p>

<p>Similar to <a href="./List::Util.html#sum">&quot;sum&quot;</a>, except this returns 0 when given an empty list, rather than <code>undef</code>.</p>

<h1 id="KEY/VALUE-PAIR-LIST-FUNCTIONS"><a class="permalink" href="./List::Util.html#KEY/VALUE-PAIR-LIST-FUNCTIONS">#</a><a id="KEY"></a><a id="KEY-VALUE-PAIR-LIST-FUNCTIONS"></a>KEY/VALUE PAIR LIST FUNCTIONS</h1>

<p>The following set of functions, all inspired by <a href="./List::Pairwise.html">List::Pairwise</a>, consume an even-sized list of pairs. The pairs may be key/value associations from a hash, or just a list of values. The functions will all preserve the original ordering of the pairs, and will not be confused by multiple pairs having the same &quot;key&quot; value - nor even do they require that the first of each pair be a plain string.</p>

<p><b>NOTE</b>: At the time of writing, the following <code>pair*</code> functions that take a block do not modify the value of <code>$_</code> within the block, and instead operate using the <code>$a</code> and <code>$b</code> globals instead. This has turned out to be a poor design, as it precludes the ability to provide a <code>pairsort</code> function. Better would be to pass pair-like objects as 2-element array references in <code>$_</code>, in a style similar to the return value of the <code>pairs</code> function. At some future version this behaviour may be added.</p>

<p>Until then, users are alerted <b>NOT</b> to rely on the value of <code>$_</code> remaining unmodified between the outside and the inside of the control block. In particular, the following example is <b>UNSAFE</b>:</p>

<pre><code>my @kvlist = ...

foreach (qw( some keys here )) {
   my @items = pairgrep { $a eq $_ } @kvlist;
   ...
}</code></pre>

<p>Instead, write this using a lexical variable:</p>

<pre><code>foreach my $key (qw( some keys here )) {
   my @items = pairgrep { $a eq $key } @kvlist;
   ...
}</code></pre>

<h2 id="pairs"><a class="permalink" href="./List::Util.html#pairs">#</a>pairs</h2>

<pre><code>my @pairs = pairs @kvlist;</code></pre>

<p><i>Since version 1.29.</i></p>

<p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of <code>ARRAY</code> references, each containing two items from the given list. It is a more efficient version of</p>

<pre><code>@pairs = pairmap { [ $a, $b ] } @kvlist</code></pre>

<p>It is most convenient to use in a <code>foreach</code> loop, for example:</p>

<pre><code>foreach my $pair ( pairs @kvlist ) {
   my ( $key, $value ) = @$pair;
   ...
}</code></pre>

<p>Since version <code>1.39</code> these <code>ARRAY</code> references are blessed objects, recognising the two methods <code>key</code> and <code>value</code>. The following code is equivalent:</p>

<pre><code>foreach my $pair ( pairs @kvlist ) {
   my $key   = $pair-&gt;key;
   my $value = $pair-&gt;value;
   ...
}</code></pre>

<p>Since version <code>1.51</code> they also have a <code>TO_JSON</code> method to ease serialisation.</p>

<h2 id="unpairs"><a class="permalink" href="./List::Util.html#unpairs">#</a>unpairs</h2>

<pre><code>my @kvlist = unpairs @pairs</code></pre>

<p><i>Since version 1.42.</i></p>

<p>The inverse function to <code>pairs</code>; this function takes a list of <code>ARRAY</code> references containing two elements each, and returns a flattened list of the two values from each of the pairs, in order. This is notionally equivalent to</p>

<pre><code>my @kvlist = map { @{$_}[0,1] } @pairs</code></pre>

<p>except that it is implemented more efficiently internally. Specifically, for any input item it will extract exactly two values for the output list; using <code>undef</code> if the input array references are short.</p>

<p>Between <code>pairs</code> and <code>unpairs</code>, a higher-order list function can be used to operate on the pairs as single scalars; such as the following near-equivalents of the other <code>pair*</code> higher-order functions:</p>

<pre><code>@kvlist = unpairs grep { FUNC } pairs @kvlist
# Like pairgrep, but takes $_ instead of $a and $b

@kvlist = unpairs map { FUNC } pairs @kvlist
# Like pairmap, but takes $_ instead of $a and $b</code></pre>

<p>Note however that these versions will not behave as nicely in scalar context.</p>

<p>Finally, this technique can be used to implement a sort on a keyvalue pair list; e.g.:</p>

<pre><code>@kvlist = unpairs sort { $a-&gt;key cmp $b-&gt;key } pairs @kvlist</code></pre>

<h2 id="pairkeys"><a class="permalink" href="./List::Util.html#pairkeys">#</a>pairkeys</h2>

<pre><code>my @keys = pairkeys @kvlist;</code></pre>

<p><i>Since version 1.29.</i></p>

<p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the first values of each of the pairs in the given list. It is a more efficient version of</p>

<pre><code>@keys = pairmap { $a } @kvlist</code></pre>

<h2 id="pairvalues"><a class="permalink" href="./List::Util.html#pairvalues">#</a>pairvalues</h2>

<pre><code>my @values = pairvalues @kvlist;</code></pre>

<p><i>Since version 1.29.</i></p>

<p>A convenient shortcut to operating on even-sized lists of pairs, this function returns a list of the the second values of each of the pairs in the given list. It is a more efficient version of</p>

<pre><code>@values = pairmap { $b } @kvlist</code></pre>

<h2 id="pairgrep"><a class="permalink" href="./List::Util.html#pairgrep">#</a>pairgrep</h2>

<pre><code>my @kvlist = pairgrep { BLOCK } @kvlist;

my $count = pairgrep { BLOCK } @kvlist;</code></pre>

<p><i>Since version 1.29.</i></p>

<p>Similar to perl&#39;s <code>grep</code> keyword, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in scalar context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p>

<p>Returns an even-sized list of those pairs for which the <code>BLOCK</code> returned true in list context, or the count of the <b>number of pairs</b> in scalar context. (Note, therefore, in scalar context that it returns a number half the size of the count of items it would have returned in list context).</p>

<pre><code>@subset = pairgrep { $a =~ m/^[[:upper:]]+$/ } @kvlist</code></pre>

<p>As with <code>grep</code> aliasing <code>$_</code> to list elements, <code>pairgrep</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p>

<h2 id="pairfirst"><a class="permalink" href="./List::Util.html#pairfirst">#</a>pairfirst</h2>

<pre><code>my ( $key, $val ) = pairfirst { BLOCK } @kvlist;

my $found = pairfirst { BLOCK } @kvlist;</code></pre>

<p><i>Since version 1.30.</i></p>

<p>Similar to the <a href="./List::Util.html#first">&quot;first&quot;</a> function, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in scalar context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p>

<p>Returns the first pair of values from the list for which the <code>BLOCK</code> returned true in list context, or an empty list of no such pair was found. In scalar context it returns a simple boolean value, rather than either the key or the value found.</p>

<pre><code>( $key, $value ) = pairfirst { $a =~ m/^[[:upper:]]+$/ } @kvlist</code></pre>

<p>As with <code>grep</code> aliasing <code>$_</code> to list elements, <code>pairfirst</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p>

<h2 id="pairmap"><a class="permalink" href="./List::Util.html#pairmap">#</a>pairmap</h2>

<pre><code>my @list = pairmap { BLOCK } @kvlist;

my $count = pairmap { BLOCK } @kvlist;</code></pre>

<p><i>Since version 1.29.</i></p>

<p>Similar to perl&#39;s <code>map</code> keyword, but interprets the given list as an even-sized list of pairs. It invokes the <code>BLOCK</code> multiple times, in list context, with <code>$a</code> and <code>$b</code> set to successive pairs of values from the <code>@kvlist</code>.</p>

<p>Returns the concatenation of all the values returned by the <code>BLOCK</code> in list context, or the count of the number of items that would have been returned in scalar context.</p>

<pre><code>@result = pairmap { &quot;The key $a has value $b&quot; } @kvlist</code></pre>

<p>As with <code>map</code> aliasing <code>$_</code> to list elements, <code>pairmap</code> aliases <code>$a</code> and <code>$b</code> to elements of the given list. Any modifications of it by the code block will be visible to the caller.</p>

<p>See <a href="./List::Util.html#KNOWN-BUGS">&quot;KNOWN BUGS&quot;</a> for a known-bug with <code>pairmap</code>, and a workaround.</p>

<h1 id="OTHER-FUNCTIONS"><a class="permalink" href="./List::Util.html#OTHER-FUNCTIONS">#</a><a id="OTHER"></a>OTHER FUNCTIONS</h1>

<h2 id="shuffle"><a class="permalink" href="./List::Util.html#shuffle">#</a>shuffle</h2>

<pre><code>my @values = shuffle @values;</code></pre>

<p>Returns the values of the input in a random order</p>

<pre><code>@cards = shuffle 0..51      # 0..51 in a random order</code></pre>

<p>This function is affected by the <code>$RAND</code> variable.</p>

<h2 id="sample"><a class="permalink" href="./List::Util.html#sample">#</a>sample</h2>

<pre><code>my @items = sample $count, @values</code></pre>

<p><i>Since version 1.54.</i></p>

<p>Randomly select the given number of elements from the input list. Any given position in the input list will be selected at most once.</p>

<p>If there are fewer than <code>$count</code> items in the list then the function will return once all of them have been randomly selected; effectively the function behaves similarly to <a href="./List::Util.html#shuffle">&quot;shuffle&quot;</a>.</p>

<p>This function is affected by the <code>$RAND</code> variable.</p>

<h2 id="uniq"><a class="permalink" href="./List::Util.html#uniq">#</a>uniq</h2>

<pre><code>my @subset = uniq @values</code></pre>

<p><i>Since version 1.45.</i></p>

<p>Filters a list of values to remove subsequent duplicates, as judged by a DWIM-ish string equality or <code>undef</code> test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p>

<pre><code>my $count = uniq @values</code></pre>

<p>In scalar context, returns the number of elements that would have been returned as a list.</p>

<p>The <code>undef</code> value is treated by this function as distinct from the empty string, and no warning will be produced. It is left as-is in the returned list. Subsequent <code>undef</code> values are still considered identical to the first, and will be removed.</p>

<h2 id="uniqint"><a class="permalink" href="./List::Util.html#uniqint">#</a>uniqint</h2>

<pre><code>my @subset = uniqint @values</code></pre>

<p><i>Since version 1.55.</i></p>

<p>Filters a list of values to remove subsequent duplicates, as judged by an integer numerical equality test. Preserves the order of unique elements, and retains the first value of any duplicate set. Values in the returned list will be coerced into integers.</p>

<pre><code>my $count = uniqint @values</code></pre>

<p>In scalar context, returns the number of elements that would have been returned as a list.</p>

<p>Note that <code>undef</code> is treated much as other numerical operations treat it; it compares equal to zero but additionally produces a warning if such warnings are enabled (<code>use warnings &#39;uninitialized&#39;;</code>). In addition, an <code>undef</code> in the returned list is coerced into a numerical zero, so that the entire list of values returned by <code>uniqint</code> are well-behaved as integers.</p>

<h2 id="uniqnum"><a class="permalink" href="./List::Util.html#uniqnum">#</a>uniqnum</h2>

<pre><code>my @subset = uniqnum @values</code></pre>

<p><i>Since version 1.44.</i></p>

<p>Filters a list of values to remove subsequent duplicates, as judged by a numerical equality test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p>

<pre><code>my $count = uniqnum @values</code></pre>

<p>In scalar context, returns the number of elements that would have been returned as a list.</p>

<p>Note that <code>undef</code> is treated much as other numerical operations treat it; it compares equal to zero but additionally produces a warning if such warnings are enabled (<code>use warnings &#39;uninitialized&#39;;</code>). In addition, an <code>undef</code> in the returned list is coerced into a numerical zero, so that the entire list of values returned by <code>uniqnum</code> are well-behaved as numbers.</p>

<p>Note also that multiple IEEE <code>NaN</code> values are treated as duplicates of each other, regardless of any differences in their payloads, and despite the fact that <code>0+&#39;NaN&#39; == 0+&#39;NaN&#39;</code> yields false.</p>

<h2 id="uniqstr"><a class="permalink" href="./List::Util.html#uniqstr">#</a>uniqstr</h2>

<pre><code>my @subset = uniqstr @values</code></pre>

<p><i>Since version 1.45.</i></p>

<p>Filters a list of values to remove subsequent duplicates, as judged by a string equality test. Preserves the order of unique elements, and retains the first value of any duplicate set.</p>

<pre><code>my $count = uniqstr @values</code></pre>

<p>In scalar context, returns the number of elements that would have been returned as a list.</p>

<p>Note that <code>undef</code> is treated much as other string operations treat it; it compares equal to the empty string but additionally produces a warning if such warnings are enabled (<code>use warnings &#39;uninitialized&#39;;</code>). In addition, an <code>undef</code> in the returned list is coerced into an empty string, so that the entire list of values returned by <code>uniqstr</code> are well-behaved as strings.</p>

<h2 id="head"><a class="permalink" href="./List::Util.html#head">#</a>head</h2>

<pre><code>my @values = head $size, @list;</code></pre>

<p><i>Since version 1.50.</i></p>

<p>Returns the first <code>$size</code> elements from <code>@list</code>. If <code>$size</code> is negative, returns all but the last <code>$size</code> elements from <code>@list</code>.</p>

<pre><code>@result = head 2, qw( foo bar baz );
# foo, bar

@result = head -2, qw( foo bar baz );
# foo</code></pre>

<h2 id="tail"><a class="permalink" href="./List::Util.html#tail">#</a>tail</h2>

<pre><code>my @values = tail $size, @list;</code></pre>

<p><i>Since version 1.50.</i></p>

<p>Returns the last <code>$size</code> elements from <code>@list</code>. If <code>$size</code> is negative, returns all but the first <code>$size</code> elements from <code>@list</code>.</p>

<pre><code>@result = tail 2, qw( foo bar baz );
# bar, baz

@result = tail -2, qw( foo bar baz );
# baz</code></pre>

<h1 id="CONFIGURATION-VARIABLES"><a class="permalink" href="./List::Util.html#CONFIGURATION-VARIABLES">#</a><a id="CONFIGURATION"></a>CONFIGURATION VARIABLES</h1>

<h2 id="$RAND"><a class="permalink" href="./List::Util.html#$RAND">#</a><a id="RAND"></a>$RAND</h2>

<pre><code>local $List::Util::RAND = sub { ... };</code></pre>

<p><i>Since version 1.54.</i></p>

<p>This package variable is used by code which needs to generate random numbers (such as the <a href="./List::Util.html#shuffle">&quot;shuffle&quot;</a> and <a href="./List::Util.html#sample">&quot;sample&quot;</a> functions). If set to a CODE reference it provides an alternative to perl&#39;s builtin <code>rand()</code> function. When a new random number is needed this function will be invoked with no arguments and is expected to return a floating-point value, of which only the fractional part will be used.</p>

<h1 id="KNOWN-BUGS"><a class="permalink" href="./List::Util.html#KNOWN-BUGS">#</a><a id="KNOWN"></a>KNOWN BUGS</h1>

<h2 id="RT-#95409"><a class="permalink" href="./List::Util.html#RT-%2395409">#</a><a id="RT"></a><a id="RT-95409"></a>RT #95409</h2>

<p><a href="https://rt.cpan.org/Ticket/Display.html?id=95409">https://rt.cpan.org/Ticket/Display.html?id=95409</a></p>

<p>If the block of code given to <a href="./List::Util.html#pairmap">&quot;pairmap&quot;</a> contains lexical variables that are captured by a returned closure, and the closure is executed after the block has been re-used for the next iteration, these lexicals will not see the correct values. For example:</p>

<pre><code>my @subs = pairmap {
   my $var = &quot;$a is $b&quot;;
   sub { print &quot;$var\n&quot; };
} one =&gt; 1, two =&gt; 2, three =&gt; 3;

$_-&gt;() for @subs;</code></pre>

<p>Will incorrectly print</p>

<pre><code class="plaintext">three is 3
three is 3
three is 3</code></pre>

<p>This is due to the performance optimisation of using <code>MULTICALL</code> for the code block, which means that fresh SVs do not get allocated for each call to the block. Instead, the same SV is re-assigned for each iteration, and all the closures will share the value seen on the final iteration.</p>

<p>To work around this bug, surround the code with a second set of braces. This creates an inner block that defeats the <code>MULTICALL</code> logic, and does get fresh SVs allocated each time:</p>

<pre><code>my @subs = pairmap {
   {
      my $var = &quot;$a is $b&quot;;
      sub { print &quot;$var\n&quot;; }
   }
} one =&gt; 1, two =&gt; 2, three =&gt; 3;</code></pre>

<p>This bug only affects closures that are generated by the block but used afterwards. Lexical variables that are only used during the lifetime of the block&#39;s execution will take their individual values for each invocation, as normal.</p>

<h2 id="uniqnum()-on-oversized-bignums"><a class="permalink" href="./List::Util.html#uniqnum()-on-oversized-bignums">#</a><a id="uniqnum1"></a><a id="uniqnum-on-oversized-bignums"></a>uniqnum() on oversized bignums</h2>

<p>Due to the way that <code>uniqnum()</code> compares numbers, it cannot distinguish differences between bignums (especially bigints) that are too large to fit in the native platform types. For example,</p>

<pre><code>my $x = Math::BigInt-&gt;new( &quot;1&quot; x 100 );
my $y = $x + 1;

say for uniqnum( $x, $y );</code></pre>

<p>Will print just the value of <code>$x</code>, believing that <code>$y</code> is a numerically- equivalent value. This bug does not affect <code>uniqstr()</code>, which will correctly observe that the two values stringify to different strings.</p>

<h1 id="SUGGESTED-ADDITIONS"><a class="permalink" href="./List::Util.html#SUGGESTED-ADDITIONS">#</a><a id="SUGGESTED"></a>SUGGESTED ADDITIONS</h1>

<p>The following are additions that have been requested, but I have been reluctant to add due to them being very simple to implement in perl</p>

<pre><code># How many elements are true

sub true { scalar grep { $_ } @_ }

# How many elements are false

sub false { scalar grep { !$_ } @_ }</code></pre>

<h1 id="SEE-ALSO"><a class="permalink" href="./List::Util.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p><a href="./Scalar::Util.html">Scalar::Util</a>, <a href="./List::MoreUtils.html">List::MoreUtils</a></p>

<h1 id="COPYRIGHT"><a class="permalink" href="./List::Util.html#COPYRIGHT">#</a>COPYRIGHT</h1>

<p>Copyright (c) 1997-2007 Graham Barr &lt;gbarr@pobox.com&gt;. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p>

<p>Recent additions and current maintenance by Paul Evans, &lt;leonerd@leonerd.org.uk&gt;.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
